package com.ziq.base.view.drawable

import android.content.res.ColorStateList
import androidx.annotation.ColorInt

/**
 * 生成ColorStateList
 * */
fun colorStateListBuild(
    block: ColorStateListBuilder.()->Unit
): ColorStateList {
    return ColorStateListBuilder().also(block).build()
}

class ColorStateListBuilder{

    private val colors: MutableList<Int> = mutableListOf()
    private val states: MutableList<IntArray> = mutableListOf()

    fun build():ColorStateList{
        return ColorStateList(states.toTypedArray(), colors.toIntArray())
    }

    /**
     * 注意添加的先后顺序，先添加的先判断状态，避免后面的状态没机会匹配
     * */
    fun colorItem(block: ColorItem.()->Unit): Unit {
        ColorItem().also(block).apply{
            if(color != null){
                colors.add(color!!)
                states.add(state)
            }
        }
    }

    class ColorItem{
        @ColorInt
        var color:Int? = null
        //android.R.attr.state_pressed = true
        //-android.R.attr.state_pressed = false // 前面加 "-"
        var state:IntArray = intArrayOf()
    }
}